/*
 * Copyright (c) 2023 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
import cardEmulation from '@ohos.nfc.cardEmulation';
import {
    GenerateKeyCBC,
    EncryptDataCBC,
    DecryptDataCBC,
    DeleteKeyCBC,
    CheckKeyExistCBC,
} from "library";

let DEFAULT_RESP = [0X6a, 0X82];
const appName = "com.harmonyos.wearable";

let RECEIVE1 = [
    0x00, 0xa4, 0x04, 0x00, 0x07,
    0xd5, 0x76, 0x00, 0x00, 0x85, 0x01, 0x01,
    0x00
];

let RESPONSE1 = [
    0x6f, 0x10, 0x84, 0x07, 0xd2, 0x76, 0x00,
    0x00, 0x85, 0x01, 0x01, 0xa5, 0x05, 0x9f,
    0x08, 0x02, 0x81, 0x31, 0x90, 0x00
];

let RECEIVE2 = [
    0x00, 0xa4, 0x00, 0x0c,
    0x02, 0xe1, 0x03
];

let RESPONSE2 = [
    0x90, 0x00
];

let RECEIVE3 = [
    0x00, 0xb0, 0x00, 0x00,
    0x0f
];

let RESPONSE3 = [
    0x00, 0x0f, 0x20, 0x01, 0x00, 0x00, 0xff,
    0x04, 0x06, 0xe1, 0x04, 0x01, 0x00, 0x00,
    0x00, 0x90, 0x00
];

let RECEIVE4 = [
    0x00, 0xa4, 0x00, 0x0c,
    0x02, 0xe1, 0x04
];

let RESPONSE4 = [
    0x90, 0x00
];

let RECEIVE5 = [
    0x00, 0xb0, 0x00, 0x00,
    0x02
];

let RESPONSE5 = [
    0x00, 0x1d, 0x90, 0x00
];

let RECEIVE6 = [
    0x00, 0xb0, 0x00, 0x02,
    0x1d
];

let RESPONSE6 = [
    0xd2, 0x0c, 0x0e,
    0x61, 0x70, 0x70, 0x2F, 0x54, 0x65, 0x73, 0x74, 0x48, 0x43, 0x45,
    0x10, 0x00, 0x00, 0x00, 0x09, 0x01,
    0x03, 0x06, 0x00, 0x00, 0x00, 0x00, 0x07,
    0xc5, 0x90, 0x00
];

let plainText = 'abcdef123456789';
let cipherText = '';

function compareArray(a1, a2) {
    if (a1 === null || a2 === null) return false;
    if (a1.length !== a2.length) return false;
    if (a1 === a2) return true;

    for (let i = 0, n = a1.length; i < n; i++) {
        if (a1[i] !== a2[i]) return false;
    }
    return true;
}

function handleApdu(apdu) {
    let response = DEFAULT_RESP;
    if (compareArray(apdu, RECEIVE1)) {
        console.log('[HceService-Logs] RESPONSE1 matched');
        response = RESPONSE1;
    } else if (compareArray(apdu, RECEIVE2)) {
        console.log('[HceService-Logs] RESPONSE2 matched');
        response = RESPONSE2;
    } else if (compareArray(apdu, RECEIVE3)) {
        console.log('[HceService-Logs] RESPONSE3 matched');
        response = RESPONSE3;
    } else if (compareArray(apdu, RECEIVE4)) {
        console.log('[HceService-Logs] RESPONSE4 matched');
        response = RESPONSE4;
    } else if (compareArray(apdu, RECEIVE5)) {
        console.log('[HceService-Logs] RESPONSE5 matched');
        response = RESPONSE5;
    } else if (compareArray(apdu, RECEIVE6)) {
        console.log('[HceService-Logs] RESPONSE6 matched');
        response = RESPONSE6;
    } else {
        console.log('[HceService-Logs] not equals, used default.');
        response = DEFAULT_RESP;
    }

    console.log('[HceService-Logs] handleApdu response = ' + response);
    return response;
}

export default {
    data: {
        headCon: appName,
        paymentAid: [
            'd5760000850100',
            'd5760000850101'
        ],
        otherAid: [
            'd4760000850102',
            'd5760000850103'
        ],
        result: ""
    },
    onInit() {

    },
    generateKey() {
        this.result = GenerateKeyCBC();
    },
    encrypt() {
        cipherText = EncryptDataCBC(plainText);
        this.result = cipherText;
    },
    decrypt() {
        this.result = DecryptDataCBC(cipherText);
    },
    deleteKey() {
        this.result = DeleteKeyCBC();
    },
    checkKeyExist() {
        this.result = CheckKeyExistCBC();
    },
    onReady() {
        cardEmulation.hasHceCapability();
        cardEmulation.isDefaultService(appName, cardEmulation.CardType.PAYMENT);
        cardEmulation.isDefaultService(appName, cardEmulation.CardType.OTHER);

        const hcesrv = new cardEmulation.HceService();

        hcesrv.start(appName, this.paymentAid);
        hcesrv.on("hceCmd", (data) => {
            console.log('[HceService-Logs] / callback => Operation hceCmd succeeded. Data: ' + data);
            let responseData = handleApdu(data);
            hcesrv.transmit(responseData, () => {
                console.log('[HceService-Logs] sendResponse start');
            });
            console.log('[HceService-Logs] sendResponse end');
        });
    }
}
